package com.zg.system.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.common.core.dao.auth.*;
import com.zg.common.core.exception.BusinessException;
import com.zg.system.auth.domain.req.RoleAddMenuReq;
import com.zg.system.auth.domain.req.RoleAddUserReq;
import com.zg.system.auth.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {
    private final AuthRoleDao roleDao;
    private final AuthRoleMenuDao roleMenuDao;
    private final AuthRoleUserDao roleUserDao;
    private final AuthMenuDao menuDao;
    private final AuthUserDao userDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(List<Long> idList) {
        roleDao.removeBatchByIds(idList);
        roleMenuDao.remove(
                new LambdaQueryWrapper<AuthRoleMenu>()
                        .in(AuthRoleMenu::getRoleId, idList)
        );
        roleUserDao.remove(
                new LambdaQueryWrapper<AuthRoleUser>()
                        .in(AuthRoleUser::getRoleId, idList)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addMenu(RoleAddMenuReq param) {
        AuthRole authRole = roleDao.getById(param.getRoleId());
        if (authRole == null) {
            BusinessException.happen("请选择正确的角色");
        }
        List<AuthMenu> authMenus = menuDao.listByIds(param.getMenuIds());
        if (authMenus == null || authMenus.size() == 0) {
            BusinessException.happen("请选择正确的菜单");
        }

        roleMenuDao.remove(
                new LambdaQueryWrapper<AuthRoleMenu>()
                        .eq(AuthRoleMenu::getRoleId, param.getRoleId())
        );

        List<AuthRoleMenu> authRoleMenus = new ArrayList<>();
        for (AuthMenu authMenu : authMenus) {
            AuthRoleMenu authRoleMenu = new AuthRoleMenu();
            authRoleMenu.setRoleId(param.getRoleId());
            authRoleMenu.setMenuId(authMenu.getId());
            authRoleMenus.add(authRoleMenu);
        }
        roleMenuDao.saveBatch(authRoleMenus);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUser(RoleAddUserReq param) {
        AuthRole authRole = roleDao.getById(param.getRoleId());
        if (authRole == null) {
            BusinessException.happen("请选择正确的角色");
        }
        List<AuthUser> authUsers = userDao.listByIds(param.getUserIds());
        if (authUsers == null || authUsers.size() == 0) {
            BusinessException.happen("请选择正确的用户");
        }

        roleUserDao.remove(
                new LambdaQueryWrapper<AuthRoleUser>()
                        .eq(AuthRoleUser::getRoleId, param.getRoleId())
        );

        List<AuthRoleUser> authRoleUsers = new ArrayList<>();
        for (AuthUser authUser : authUsers) {
            AuthRoleUser authRoleUser = new AuthRoleUser();
            authRoleUser.setRoleId(param.getRoleId());
            authRoleUser.setUserId(authUser.getId());
            authRoleUsers.add(authRoleUser);
        }
        roleUserDao.saveBatch(authRoleUsers);
    }
}
